<?php

class Filter_Admin_Form_Create extends Engine_Form {

    public function __construct($options = null) {
        parent::__construct($options);

        $this->_postInit();
    }
    
    // set isValid for elements in every area
    public function isValid($data) 
    {
        if ('blog' == $data['filter_type']) {
            //
        } elseif ('event' == $data['filter_type']) {
            //$this->title->setRequired(false);
        }


        return parent::isValid($data);
    }

    public function init() 
    {
        $this->setTitle('Create Filter')
            ->setAttrib('class', 'global_form_popup');
        
        // Get array types from engine4_filter_types
        $typeTable = Engine_Api::_()->getItemTable('filter_type');
        $select = $typeTable->select()
                            ->from($typeTable, array('type_id', 'title'));
        
        $this->addElement('Select', 'filter_type', array(
            'label' => 'Search Area',
            'multiOptions' => $typeTable->getAdapter()->fetchPairs($select),
            'onChange' => 'processForm(this)'
        ));
        
        $this->addElement('Text', 'title', array(
            'label' => 'Filter Name',
            'required' => true,
            'filters' => array(
                new Engine_Filter_Censor(),
                'StripTags',
                new Engine_Filter_StringLength(array('max' => '51'))
            ),
        ));
        
        // get all criteria by id
        $types = $typeTable->fetchAll($select);
        
        foreach ($types as $type) {
            // Get criteria by type_id
            $criteria = Engine_Api::_()->getDbTable('criteria', 'filter')->fetchAll(array('type_id = ?' => $type->getIdentity()));
            foreach($criteria as $item){
                if ($item->element_type == 'calendar') {
                    $element = new Engine_Form_Element_CalendarDateTime("item_" . $item->getIdentity()."_".$item->type_id."_".$item->element_type);
                    $element->setLabel($item->title);
                    //$element->setValue(date('Y-m-d H:i:s'));
                    $element->setAttrib('id', "type_" . $item->type_id . "-item_" . $item->getIdentity(). "-". $item->element_type);
                    $this->addElement($element);
                } elseif($item->element_type == 'selectbox') {
                    //
                } 
                else {
                    $this->addElement($item->element_type, "item_" . $item->getIdentity()."_".$item->type_id."_".$item->element_type, array(
                        'label' => $item->title,
                        'attribs' => array('id' => "type_" . $item->type_id . "-item_" . $item->getIdentity(). "-". $item->element_type)
                    ));
                }
            }
        }
        
        $this->addElement('Button', 'submit', array(
            'label' => 'Save',
            'type' => 'submit',
            'ignore' => true,
            'decorators' => array(
                'ViewHelper',
            ),
        ));

        $this->addElement('Cancel', 'cancel', array(
            'label' => 'cancel',
            'type' => 'button',
            'link' => true,
            'onclick' => 'parent.Smoothbox.close();',
            'prependText' => ' or ',
            'decorators' => array(
                'ViewHelper',
            ),
        ));

        $this->addDisplayGroup(array('submit', 'cancel'), 'buttons', array(
            'decorators' => array(
                'FormElements',
                'DivDivDivWrapper',
            ),
        ));
    }

    public function _postInit() 
    {
        $tmp = '';
        $tmp .= 'if (el.value != "") {';
        $tmp .= 'var type_id = el.value;';
        
        foreach ($this->_elements as $name => $element) {
            
            if (in_array($name, array('filter_type', 'submit', 'cancel', 'buttons'))) {
                continue;
            }
            if($element->getType() == 'Engine_Form_Element_CalendarDateTime'){
                
                $tmp .= 'if($(\'' . $name . '-wrapper\').getElements(\'label\')[0]){';
                $tmp .= 'var element_type_id = $(\'' . $name . '-wrapper\').getElements(\'label\')[0].getAttribute("for").split("-")[0];';
                $tmp .= 'if(element_type_id == "type_" + type_id){'
                        . '$(\'' . $name . '-wrapper\').setStyle(\'display\',\'block\');'
                        . '}else{'
                        . '$(\'' . $name . '-wrapper\').setStyle(\'display\',\'none\');'
                        . '}';
                $tmp .= '}';
                
                if ('smoothbox' == Zend_Controller_Front::getInstance()->getRequest()->getParam('format')) {
                    $tmp .= 'parent.Smoothbox.instance.doAutoResize();';
                }
            }else{
                $tmp .= 'if($("'. $element->id .'")){';
                $tmp .= 'var element_type_id = $("'. $element->id .'").id.split("-")[0];';
                $tmp .= 'if(element_type_id == "type_" + type_id){'
                        . '$(\'' . $name . '-wrapper\').setStyle(\'display\',\'block\');'
                        . '}else{'
                        . '$(\'' . $name . '-wrapper\').setStyle(\'display\',\'none\');'
                        . '}';
                $tmp .= '}';
                
                if ('smoothbox' == Zend_Controller_Front::getInstance()->getRequest()->getParam('format')) {
                    $tmp .= 'parent.Smoothbox.instance.doAutoResize();';
                }
            }
            
        }

        $tmp .= '}';
        $script = 'function processForm(el){' . $tmp . '}
            window.addEvent("domready", function(){ processForm($("filter_type")); });
        ';
        $this->getView()->headScript()->appendScript($script);
    }

}
